package net.towee.server.rpc.servlet;

import java.lang.reflect.Method;
import java.util.List;

import net.towee.model.account.AccountGroup;
import net.towee.model.account.AccountIdentity;
import net.towee.model.account.AccountProfile;
import net.towee.model.account.Role;
import net.towee.model.discussion.Message;
import net.towee.model.group.GroupIdentity;
import net.towee.model.repo.DocumentData;
import net.towee.model.repo.DocumentIdentity;
import net.towee.model.repo.NodeData;
import net.towee.model.utils.Pair;
import net.towee.server.authentication.SessionAuthManager;
import net.towee.server.rpc.ToweeSocketServlet;
import net.towee.server.rpc.services.ToweeService;

import org.apache.vysper.xmpp.server.ServerRuntimeContext;

import com.mycila.event.Dispatcher;

/**
 * This abstract class contains realization of push methods.
 * It sould be generated while generating client proxy classes
 * 
 * @author c58
 */
@SuppressWarnings("serial")
public abstract class AbstractToweeService extends ToweeSocketServlet implements ToweeService {
	
	public AbstractToweeService(Dispatcher dispatcher, SessionAuthManager sessionManager, ServerRuntimeContext xmppServer) {
		super(dispatcher, sessionManager, xmppServer);
	}
	
	private Method getMethod(String name, Class<?> response) {
		try {
			return ToweeService.class.getMethod(name, response, String.class);
		} catch (SecurityException e) {
			throw new RuntimeException(e);
		} catch (NoSuchMethodException e) {
			throw new RuntimeException(e);
		}
	}
	
	@Override
	public AccountIdentity onAccountIdentityUpdated(AccountIdentity result, String node) {
		publishMessage( getMethod("onAccountIdentityUpdated", AccountIdentity.class), result, node);
		return null;
	}
	
	@Override
	public AccountProfile onAccountProfileUpdated(AccountProfile result, String node) {
		publishMessage( getMethod("onAccountProfileUpdated", AccountProfile.class), result, node);
		return null;
	}
	
	@Override
	public Pair<String, Boolean> onOnlineStateChanged(Pair<String, Boolean> result, String node) {
		publishMessage( getMethod("onOnlineStateChanged", Pair.class), result, node);
		return null;		
	}
	
	@Override
	public Pair<AccountIdentity, String> onUserAddedToContacts(Pair<AccountIdentity, String> result,
			String node) {
		publishMessage( getMethod("onUserAddedToContacts", Pair.class), result, node);
		return null;
	}

	@Override
	public Pair<AccountIdentity, String> onUserRemovedFromContacts(Pair<AccountIdentity, String> result,
			String node) {
		publishMessage( getMethod("onUserRemovedFromContacts", Pair.class), result, node);
		return null;
	}
	
	@Override
	public Pair<String, Message> onDiscussionMessageUpdate(Pair<String, Message> result, String node) {
		publishMessage( getMethod("onDiscussionMessageUpdate", Pair.class), result, node);
		return null;
	}
	
	@Override
	public DocumentData onDocumentUpdate(DocumentData doc, String node) {
		publishMessage( getMethod("onDocumentUpdate", DocumentData.class), doc, node);
		return null;
	}

	@Override
	public List<DocumentIdentity> onDocumentDelete(List<DocumentIdentity> identity, String node) {
		publishMessage( getMethod("onDocumentDelete", List.class), identity, node);
		return null;
	}

	@Override
	public Pair<AccountIdentity, DocumentIdentity> onDocumentRated(
			Pair<AccountIdentity, DocumentIdentity> result, String node) {
		publishMessage( getMethod("onDocumentRated", Pair.class), result, node);
		return null;
	}
	
	@Override
	public GroupIdentity onUserLeaveFromGroup(GroupIdentity result, String node) {
		publishMessage( getMethod("onUserLeaveFromGroup", GroupIdentity.class), result, node);
		return null;
	}

	@Override
	public AccountGroup onUserEnterToGroup(GroupIdentity result, String node) {
		publishMessage( getMethod("onUserEnterToGroup", GroupIdentity.class), result, node);
		return null;
	}
	
	@Override
	public List<NodeData> onNodesRemoved(List<NodeData> nodes, String node) {
		publishMessage( getMethod("onNodesRemoved", List.class), nodes, node);
		return null;
	}

	@Override
	public List<NodeData> onNodesChange(List<NodeData> treeNode, String node) {
		publishMessage( getMethod("onNodesChange", List.class), treeNode, node);
		return null;
	}

	@Override
	public List<NodeData> onNodesToTrash(List<String> nodeIds, String node) {
		publishMessage( getMethod("onNodesToTrash", List.class), nodeIds, node);
		return null;
	}

	@Override
	public Pair<String, List<NodeData>> onNodesMoved(Pair<String, List<NodeData>> result, String node) {
		publishMessage( getMethod("onNodesMoved", Pair.class), result, node);
		return null;
	}
	
	@Override
	public Pair<Role, String> onUserRoleUpdated(Pair<Role, String> result, String node) {
		publishMessage( getMethod("onUserRoleUpdated", Pair.class), result, node);
		return null;
	}
}
